package com.qianfeng.smartdevice.providers;

import org.springframework.util.StringUtils;

public class UserSqlProvider {

    public String getQueryByNameAndStaus(String username, Integer status) {
        StringBuffer stringBuffer = new StringBuffer("SELECT * FROM users WHERE");
        boolean isNeedAnd = false;
        if (!StringUtils.isEmpty(username)) {
            stringBuffer.append("username = #{username}");
            isNeedAnd = true;
        }

        if (status!=null) {
            if (isNeedAnd) {
                stringBuffer.append(" and status = #{status}");
            }else{
                stringBuffer.append("  status = #{status}");
            }
        }

        String sql = stringBuffer.toString();
        if (sql.endsWith("WHERE")) {
            sql.replace("WHERE", "");
        } else if (sql.endsWith("and")) {
            sql.replace("and", "");
        }
        return sql;
    }

}
